// pages/studyRecord/grammarReview/grammarReview.js
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');

var list = [{ "id": 70, "title": "September is the ### month of a year. ", "keys_words": "ninth", "translation": "九月是一年的第九个月份", "voice": "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/grammar/septem.mp3" },
{ "id": 727, "title": "Berlin is ### of Germany . ", "keys_words": "the capital ", "translation": "柏林是德国的首都。", "voice": "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/grammar/berlin.mp3" },
{ "id": 728, "title": "### you know when he ### .", "keys_words": "I’ll let,comes ", "translation": "他来时我会告诉你的。", "voice": "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/grammar/berlin.mp3" },
{ "id": 729, "title": "Don’t be late again, ### you? ### , I ### .", "keys_words": "will,No,won’t ", "translation": "他来时我会告诉你的。", "voice": "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/grammar/berlin.mp3" }
];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    grammarlist: [],
    hasreview: 0,
    date: '',
    task_id:'', //任务id
    sort:'9', //类别
    page: 1,
    pageSize: 200,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this;
     if(options){
      that.setData({
        task_id: options.task_id,
        sort: options.sort,
        date:options.date
      })
     }
     console.log("------------------date="+that.data.date)
    that.getData();

    // that.dealSentence();
  },

  getData: function () {
    let that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true
    })

    httpUtils.getGrammerReview({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      calendar_id: that.data.task_id,
      page:1,
      size:that.data.pageSize,
      sort:that.data.sort,
    }).then((res) => {
      //假数据
      // let temparr = that.data.grammarlist;
      let temparr = res.data.lists;
      that.setData({
        grammarlist: temparr,
      })
      that.dealSentence();
    });

  },

  //处理句子
  dealSentence: function () {
    let that = this;
    let temparr = that.data.grammarlist;
    for (let i = 0; i < temparr.length; i++) {
      let title = temparr[i].replacetitle.trim();
      // let splitArry = title.split("###");
      let splitArry = title.split(" ");
      temparr[i].splitArry = splitArry;

      console.log("splitArry=" + JSON.stringify(splitArry));
    }

    that.setData({
      grammarlist: temparr,
    })
  },

  getInputValue: function (e) {
    let invalue = e.detail.value;
    return invalue.replace(/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g, '');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  // 完成提交语法复习
  submitReview: function (e) {
    console.log("submitReview" + JSON.stringify(e))
    let that = this;
    let allSentence = that.data.grammarlist;

    //所有input输入的值
    let inputvalue = e.detail.value;
    console.log(JSON.stringify(inputvalue));
    var arr = []
    for (let key in inputvalue) {
      // 每遍历一次，就创建一个字典对象
      // let dict = {};
      // dict[key] = inputvalue[key];
      // arr.push(dict);
      arr.push(inputvalue[key]);
    }
    console.log("arr:" + JSON.stringify(arr));
   let emptyNum=0;
   for(let r=0;r<arr.length;r++){
      if(arr[r]==""){
        emptyNum++
      }
   }

   if(emptyNum ==arr.length){
    wx.showToast({
      title: '请先答题再提交',
      icon: 'none',
    })
    return;
   }
  
  
    let answerList = [];
    let allanswer = arr;

    for (let j = 0; j < allSentence.length; j++) {
      let uanswer = new Object();
      let id = allSentence[j].id;
      uanswer['id'] = id;
      let answercontent = "";
      let keywordarr = allSentence[j].keys_words.split(",");
      console.log("keywordarr", JSON.stringify(keywordarr));
      for (let k = 0; k < keywordarr.length; k++) {
        if (allanswer[k]) {
          answercontent = answercontent + allanswer[k] + ",";
        }
      }
      uanswer['title'] = answercontent;
      answerList.push(uanswer);
      console.log("answercontent:", answercontent);
      allanswer.splice(0, keywordarr.length);

    }
    console.log(JSON.stringify(answerList));

    let tempStudy = '';
    for (var i in allSentence) {
      if (tempStudy == '') {
        tempStudy = allSentence[i].id
      } else {
        tempStudy = tempStudy + "," + allSentence[i].id;
      }
    }
    console.log("tempStudy为：", tempStudy);
    wx.showLoading({
      title: '正在提交',
      mask: true
    })

    httpUtils.submitGrammarAnswer({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      sentence_id: tempStudy,
      calendar_id: that.data.task_id,
      sort:that.data.sort,
      stu_answer: JSON.stringify(answerList)
    }).then((res) => {
      //如果提交成功，跳转报告页
      console.log(JSON.stringify(res))
      let report = JSON.stringify(res)
      wx.redirectTo({
        url: '/pages/studyRecord/grammarReport/grammarReport?report=' + report+'&date='+that.data.date,
      })

    });
  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})